#include <stdio.h>  
#include <string.h>  
  
struct person {  
    char name[105];  
    int score;  
} stu[105];  
  
int main() {  
    int n;  
    if (scanf("%d", &n));  
    getchar();  
    for (int i = 0; i < n; i++) {  
        int cnt = 0;  
        while (scanf("%c", &stu[i].name[cnt++]))  
            if (stu[i].name[cnt - 1] == ',') {  
                stu[i].name[cnt] = '\0';  
                break;  
            }  
        if (scanf("%d", &stu[i].score));  
        getchar();  
    }  
  
    for (int i = 0; i < n; i++) {  
        for (int j = n - 1; j > i; j--)  
            if (stu[j - 1].score < stu[j].score) {  
                int temp = stu[j - 1].score;  
                stu[j - 1].score = stu[j].score;  
                stu[j].score = temp;  
                char tmp[105];  
                strcpy(tmp, stu[j - 1].name);  
                strcpy(stu[j - 1].name, stu[j].name);  
                strcpy(stu[j].name, tmp);  
            }  
    }  
    for (int i = 0; i < n; i++)  
        printf("%s%d\n", stu[i].name, stu[i].score);  
    return 0;  
}  